define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Panicauctiondetail,  $window, $stateParams,$timeout, $ionicSlideBoxDelegate,$ionicPopup,$interval,GoPage,GoDialog,GoShare,GoWapAuth) {
		$scope.act_id = $stateParams.act_id;
		$scope.goods_id = $stateParams.goods_id;
		$scope.product_id = $stateParams.product_id;
		$scope.isLoad = false; //可以加载
		$scope.input = new Object();
		$scope.act_count = 0;
		$scope.msg = '';
		$scope.input.checked = true;
		$scope.set_time = false;
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			//动态设置高度
			var all = $window.innerHeight;
			//计算滚动条
			$scope.myObj = {
				"height": all - 44 + "px"
			};
			$scope.init_data();
			$scope.set_time = false;
		}, false);
		
		//滑动时候触发
		$scope.selectTab = 0;
		$scope.activeTab = function(index) {
			$scope.selectTab = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
		};
		$scope.slideChanged = function(index) {
			//手指滑动
			$scope.selectTab = index;
		};
		$scope.onshow_first = function(store_id) {
			GoPage.go_states("first", {
				"store_id": store_id
			});
		};
		$scope.$watch('input.number', function(newValue, oldValue) {
			if(newValue == oldValue) return;
			else {
				if($scope.input.number< Number($scope.datas.start_price_dis) ){
					$scope.input.number = Number($scope.datas.start_price_dis);
				}else{
					var dec = $scope.input.number-Number($scope.datas.start_price_dis);
					if(dec%Number($scope.datas.amplitude)!=0){
						var i = parseInt(dec/Number($scope.datas.amplitude)) +1;
						$scope.input.number = Number($scope.datas.start_price_dis)+i*Number($scope.datas.amplitude);
					}
				}
			}

		})
		$scope.goods_add = function() {
			$scope.input.number += Number($scope.datas.amplitude);
		};
		$scope.goods_reduce = function() {
			if($scope.input.number > $scope.datas.start_price_dis)
				$scope.input.number -= Number($scope.datas.amplitude);

		};
		$scope.formate_time = function(time) {
			if(time > 0) {
				var day = parseInt(time / (3600 * 24));
				var hour = parseInt((time % (3600 * 24)) / 3600);
				if(hour < 10)
					hour = "0" + hour;
				var min = parseInt((time % 3600) / 60);
				if(min < 10)
					min = "0" + min;
				var second = time % 60;
				if(second < 10)
					second = "0" + second;
				return day + '天' + hour + '小时' + min + '分' + second + '秒';
			} else {
				$scope.isshow = false;
				$scope.btn_name = '已结束';
				$scope.btn_type = 5;
				$scope.img_show = false;
				return '0天0小时0分0秒';
			}
		};
		$scope.init_time = function(data, now_time) {
			if(now_time < data.start_time) {
				$scope.isshow = false;
				$scope.btn_name = '未开始';
				$scope.act_time_name = '距开始';
				if(data.now_time != undefined)
					data.now_time = data.now_time - 1;
				else
					data.now_time = data.start_time - now_time;
			} else {
				$scope.act_time_name = '距结束';
				if(data.now_time != undefined)
					data.now_time = data.now_time - 1;
				else
					data.now_time = data.end_time - now_time;

			}
			data.formate_time = $scope.formate_time(data.now_time);
			$scope.set_timer(now_time);
		};
		$scope.set_timer = function(now_time) {
//			console.log(now_time);
if(!$scope.set_time){
				$scope.set_time	 = true;
			}else{
				return;
			}
			$interval(function() {
				if($scope.datas.now_time != undefined){
					$scope.datas.now_time = $scope.datas.now_time - 1;
					if($scope.datas.now_time<=0){
						if(now_time >= $scope.datas.start_time) {
						$scope.datas.now_time = $scope.datas.end_time - now_time;
						$scope.act_time_name = '距结束';
					} else {
						$scope.datas.now_time = $scope.datas.start_time - now_time;
						$scope.act_time_name = '距开始';
					}
					}
				}
				else {
					if(now_time > $scope.datas.start_time) {
						$scope.datas.now_time = $scope.datas.end_time - now_time;
						$scope.act_time_name = '距结束';
					} else {
						$scope.datas.now_time = $scope.datas.start_time - now_time;
						$scope.act_time_name = '距开始';
					}
				}
				now_time++;
				$scope.datas.formate_time = $scope.formate_time($scope.datas.now_time);
			}, 1000)
		};
		//加载数据
		$scope.init_data = function() {
			Panicauctiondetail.auction_view($scope.act_id,$scope.goods_id,$scope.product_id,function(data,now_time){
				if(data.success){
					$scope.datas = data.result;
				$scope.input.number = Number($scope.datas.start_price_dis);
				$scope.style = "shop_grade" + " " + "grade" + data.result.store_level;
				$scope.init_time(data.result,now_time);
				$ionicSlideBoxDelegate.$getByHandle('advs-handle').update(); //解决无法加载
				$ionicSlideBoxDelegate.$getByHandle('advs-handle').loop(true); //循环
				$scope.go_share_wx();
				}else{
					$scope.msg = data.msg;
				}
				
			})
		}
//		$scope.init_data();
		//轮播小圆点切换
		$scope.advsgo = function(index) {
			$ionicSlideBoxDelegate.$getByHandle('advs-handle').slide(index);
		}
		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			$scope.init_data();
			$timeout(function() {
				$scope.$broadcast('scroll.refreshComplete');
			}, 1000);
		};
		//加载更多
		$scope.doRefreshMore = function() {
			$timeout(function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
			}, 1000);
		};
		var myPopup;
		$scope.onshow_productInfo = function(index, goods_id) {
			GoPage.go_states("panicauction_pinfo", {
				"id": index,
				"act_id": $scope.act_id,
				"product_id": $scope.product_id,
				"is_price":$scope.datas.is_price
			});
		}
		$scope.show_apply = function(){
			if($scope.datas.isOwner=='1'){
				GoDialog.toast('自己店铺的拍品不能出价');
				return;
			}
			myPopup = $ionicPopup.show({ //show()
					templateUrl: 'templates/modal/auction_apply.html',
					title: "提交保证金",
					scope: $scope, //绑定数据，必须写这行
					buttons: [{
						text: "确定",
						type: "button-assertive",
						onTap: function(e) { // 当点击保存按钮时，执行该函数
							if(!$scope.input.checked){
								GoDialog.toast("请同意竞拍协议再提交保证金！");
								e.preventDefault();
								return;
							}
							var account = 0;
							GoPage.go_states('panicauction_pay',{'act_id':$scope.act_id,'apply_id':$scope.datas.apply_id,'goods_id':$scope.goods_id,'product_id':$scope.product_id});
						}
					},{
					text: "取消",
						type: "button-assertive",
						onTap: function() { // 当点击保存按钮时，执行该函数
							//						console.log($scope.scope.reason);
							//return $scope.scope.reason;//
						}	
					}]
				});
		}
		var aPopup;
		$scope.show_article = function(index){
			myPopup.close();
			if(index==101){
				$scope.title = '竞拍协议';
			}else{
				$scope.title = '保证金归属规则'
			}
			aPopup = $ionicPopup.show({ //show()
					templateUrl: 'templates/modal/auction_article.html',
					title: $scope.title,
					scope: $scope, //绑定数据，必须写这行
					buttons: [{
						text: "确定",
						type: "button-assertive",
						onTap: function(e) { // 当点击保存按钮时，执行该函数
							aPopup.close();
//							myPopup.show();
							$scope.show_apply();
						}
					}]
				});
		}
		$scope.go_record = function(){
			GoPage.go_states('predepositwater');
		}
		$scope.get_article = function(index){
			Panicauctiondetail.get_article(index,function(data){
				if(data.success==true){
					$scope.article = data.result.content;
				}else{
					$scope.article = data.msg;
				}
				$scope.show_article(index);
				
			})
		}
		$scope.apply =function(){
			if($scope.datas.isOwner=='1'){
				GoDialog.toast('自己店铺的拍品不能出价');
				return;
			}
			Panicauctiondetail.auction_apply($scope.act_id,$scope.input.number,function(data){
				if(data.success == true){
					$scope.datas.last_price = data.result.cur_price_dis;
					$scope.input.number = Number(data.result.cur_price_dis)+Number($scope.datas.amplitude);
				}else{
					$scope.datas.last_price = data.result.cur_price_dis;
					$scope.act_count = Number(data.result.act_count); 
					$scope.input.number = Number(data.result.cur_price_dis)+Number($scope.datas.amplitude);
				}
				GoDialog.toast(data.msg);
			})
		}
		//页面离开，分享关闭
		$scope.$on('$ionicView.afterLeave', function() {
			$scope.closeGuidShare();
		}, false);
		$scope.isHide = true;
		//隐藏弹窗
		$scope.hide_apply = function(){
			$scope.isHide = true;
		}
		//显示弹窗
		$scope.share_apply = function(value){
			if($scope.openGuidShare()){
				$scope.isHide = false;
				$scope.word_id = value;
			}
		}
		//分享（自定义每个页面的参数）
		$scope.go_share = function(index) {
			//			console.log(index);
			var param = new Object();
			if($scope.datas) {
				param.title = $scope.datas.act_name ? $scope.datas.act_name : undefined;
				param.desc = $scope.datas.introduction ? $scope.datas.introduction : undefined;
				param.thumb = $scope.datas.image ? $scope.datas.image[0].image : undefined;
				param.link = 'panicauctiondetail/'+$scope.act_id+'/' + $scope.goods_id+'/'+$scope.product_id;
			}
			console.log(param);
			GoShare.do_share(param, index);
		}
														//分享（自定义每个页面的参数）
		$scope.go_share_wx = function() {
			//分享打开
			GoWapAuth.execisoff(false);
			//设置分享
			var param = new Object();
			if($scope.datas) {
				param.title = $scope.datas.act_name ? $scope.datas.act_name : undefined;
				param.desc = $scope.datas.introduction ? $scope.datas.introduction : undefined;
				param.thumb = $scope.datas.image ? $scope.datas.image[0].image : undefined;
				param.link = 'panicauctiondetail/'+$scope.act_id+'/' + $scope.goods_id+'/'+$scope.product_id;
			}
			GoWapAuth.execshare(param);
		}
	}

	controller.$inject = ['$scope', 'Panicauctiondetail',  '$window','$stateParams','$timeout', '$ionicSlideBoxDelegate','$ionicPopup','$interval','GoPage','GoDialog','GoShare','GoWapAuth'];

	return app.registerController("panicauctiondetailctrl",controller);
});